package com.toolmanrln.common.api;

import com.alibaba.fastjson.annotation.JSONField;
import com.toolmanrln.common.base.IEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

/**
 * @author ToolMan.RLN
 * @since 2020-05-15 18:06
 */
@Data
@Accessors(chain = true)
@ApiModel(value = "请求结果", description = "请求结果包装类")
public class ResponseData<T> {

    /**
     * 请求是否成功
     */
    @ApiModelProperty(value = "成功标识；true:成功 false:失败")
    private Boolean success;

    /**
     * 响应状态码
     */
    @ApiModelProperty(value = "返回状态码；200:成功,500:失败")
    private Integer code;

    /**
     * 响应信息
     */
    @ApiModelProperty(value = "响应信息")
    private String message;

    /**
     * 响应对象
     */
    @ApiModelProperty(value = "响应对象")
    private T data;

    @ApiModelProperty(value = "服务器响应时间，格式为 yyyy-MM-dd HH:mm:ss")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime time = LocalDateTime.now();

    public ResponseData() {
    }

    public ResponseData(Boolean success, Integer code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static ResponseData success() {
        return new ResponseData(true, ResultCodeEnum.SUCCESS.getKey(), ResultCodeEnum.SUCCESS.getValue(), null);
    }

    public static ResponseData success(Object object) {
        return success().setData(object);
    }

    public static ResponseData success(Integer code, String message, Object object) {
        return new ResponseData().setCode(code).setMessage(message).setData(object);
    }

    public static ResponseData failed() {
        return new ResponseData(false, ResultCodeEnum.FAILED.getKey(), ResultCodeEnum.FAILED.getValue(), null);
    }

    public static ResponseData failed(String message) {
        return new ResponseData(false, ResultCodeEnum.FAILED.getKey(), message, null);
    }

    public static ResponseData failed(Integer code, String message) {
        return new ResponseData(false, code, message, null);
    }

    public static ResponseData failed(Integer code, String message, Object object) {
        return new ResponseData(false, code, message, object);
    }

    public static ResponseData failed(IEnum iEnum) {
        return failed(iEnum.getKey(), iEnum.getValue());
    }

}
